<template>
  <div class="app-container">
    <el-form ref="form" inline :model="form" class="form-options" label-width="100px">
      <el-form-item label="仓库名称">
        <el-autocomplete
          ref="nameRef"
          v-model="form.name"
          :fetch-suggestions="querySearchName"
          :trigger-on-focus="false"
          clearable
          @select="handleSelectName"
          @clear="clickClearName"/>
      </el-form-item>
      <el-form-item label="归属单位">
        <el-autocomplete
          ref="departmentNameRef"
          v-model="form.departmentName"
          :fetch-suggestions="querySearchDepartmentName"
          :trigger-on-focus="false"
          clearable
          @select="handleSelectDepartmentName"
          @clear="clickClearDepartmentName"/>
      </el-form-item>
      <div class="btns">
        <el-button type="primary" v-permission="['base_manage_query']" @click="onQuery">查询</el-button>
      </div>
    </el-form>
    <el-table
      :data="tableData"
      header-cell-class-name="table-header"
      style="width: 100%;margin-top: 20px"
      show-overflow-tooltip>
      <el-table-column
        label="序号"
        type="index"
        width="50">
      </el-table-column>
      <el-table-column
        prop="name"
        label="类型">
      </el-table-column>
      <el-table-column
        prop="typeName"
        label="类型名称">
      </el-table-column>
      <el-table-column
        prop="departmentName"
        label="归属单位">
      </el-table-column>
      <el-table-column
        prop="address"
        label="地址">
      </el-table-column>
      <el-table-column
        label="经纬度">
        <template slot-scope='scope'>
          {{ scope.row.longitude }}，{{ scope.row.latitude }}
        </template>
      </el-table-column>
      <el-table-column
        prop="phone"
        label="电话">
      </el-table-column>
      <el-table-column
        prop="remarks"
        label="备注">
      </el-table-column>
    </el-table>
    <div style="text-align: right;margin-top: 20px">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 15, 30]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getWarehouse } from '@/api/warehouse'
import { getOrganizations } from '@/api/user'

export default {
  name: 'WarehouseManage',
  data() {
    return {
      currentPage: 1,
      total: 0,
      pageSize: 10,
      form: {
        departmentName: '',
        name: ''
      },
      tableData: []
    }
  },
  created() {
    this.onQuery()
  },
  methods: {
    handleSizeChange: function(val) {
      this.pageSize = val
      this.currentPage = 1
      this.getTableData()
    },
    handleCurrentChange: function(val) {
      this.currentPage = val
      this.getTableData()
    },
    /**
     * 模糊查询归属单位
     * @param queryString
     * @param cb
     */
    querySearchDepartmentName: function(queryString, cb) {
      // 调用 callback 返回建议列表的数据
      const results = []
      getOrganizations({ name: queryString }).then(res => {
        res.resultData.forEach(item => {
          if (results.length < 10) {
            results.push({ value: item.name })
          }
        })
        cb(results)
      })
    },
    /**
     * 选择归属单位
     * @param item
     */
    handleSelectDepartmentName(item) {
      this.form.departmentName = item.value
    },
    /**
     * 模糊查询用户名称
     * @param queryString
     * @param cb
     */
    querySearchName: function(queryString, cb) {
      // 调用 callback 返回建议列表的数据
      const results = []
      cb(results)
    },
    /**
     * 选择用户名称
     * @param item
     */
    handleSelectName(item) {
      this.form.name = item.value
    },
    clickClearName(){
      this.$nextTick(() => {
        if (this.$refs.nameRef) {
          this.$refs.nameRef.activated = true;
        }
      });
    },
    clickClearDepartmentName(){
      this.$nextTick(() => {
        if (this.$refs.departmentNameRef) {
          this.$refs.departmentNameRef.activated = true;
        }
      });
    },
    getTableData() {
      const param = { page: this.currentPage, rows: this.pageSize }
      Object.assign(param, this.form)
      getWarehouse(param).then(res => {
        this.tableData = res.resultData
        this.total = res.total
        this.multipleSelection = []
      })
    },
    onQuery() {
      this.currentPage = 1
      this.getTableData()
    }
  }
}
</script>

<style lang="scss" scoped>
.line {
  text-align: center;
}
</style>

